<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams"
             label-width="120px" size="large">
      <el-form-item label="所属子系统" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          clearable
          placeholder="所属子系统"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属分类" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          placeholder="请输入分类(鱼类、虾蟹类、贝类、棘皮类)"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          @click="handleUpdate"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tree :data="data" :props="defaultProps"></el-tree>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改养殖种类分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body center="true" width="750px">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="所属子系统" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :normalizer="normalizer"
            :options="menuOptions"
            :show-count="true"
            placeholder="选择上级菜单"
          />
        </el-form-item>
        <el-form-item label="所属分类" prop="name">
          <el-input v-model="form.name" placeholder="请输入所属分类（鱼类、虾蟹类、贝类、棘皮类）"/>
        </el-form-item>
        <el-form-item label="塘口价（元/KG）" prop="name">
          <el-input v-model="form.threshingPrice" placeholder="请输入塘口价"/>
        </el-form-item>
        <el-form-item label="批发价（元/KG）" prop="name">
          <el-input v-model="form.wholesalePrice" placeholder="请输入批发价"/>
        </el-form-item>
        <el-form-item label="零售价（元/KG）" prop="name">
          <el-input v-model="form.retailPrice" placeholder="请输入零售价"/>
        </el-form-item>
        <el-form-item label="自取价（元/KG）" prop="name">
          <el-input v-model="form.ziquPrice" placeholder="请输入自取价格"/>
        </el-form-item>
        <el-form-item label="送货价（元/KG）" prop="name">
          <el-input v-model="form.songhuoPrice" placeholder="请输入送货价格"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addCulture, delCulture, getCulture, listCulture, updateCulture} from "@/api/system/culture";
import Treeselect from '@riophae/vue-treeselect';
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listQueryCulture} from "../../../api/system/culture";

export default {
  name: "Culture",
  components: {Treeselect},
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      menuOptions: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 养殖种类分类表格数据
      cultureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        name: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          {required: true, message: "分类父主键id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询养殖种类分类列表 */
    getList() {
      this.loading = true;
      listCulture(this.queryParams).then(response => {
        this.data = response.rows;
        this.cultureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleNodeClick(data) {
      this.form.parentId = data.id;
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listQueryCulture().then(response => {
        this.menuOptions = [];
        const menu = {id: 0, name: '主类目', children: []};
        menu.children = this.handleTree(response.rows, "id");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        name: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加养殖种类分类";
      this.getTreeselect();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCulture(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改养殖种类分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCulture(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCulture(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除养殖种类分类编号为"' + ids + '"的数据项？').then(function () {
        return delCulture(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/culture/export', {
        ...this.queryParams
      }, `culture_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-form-item__label {
  font-size: 17px;
}

.el-dialog__title {
  font-size: 35px;
  font-family: 华文楷体;
  color: orangered;
}

.el-button {
  font-size: 20px;
}
</style>
